  <template>
  <div class="category">
    <div class="category_top">
      <el-button type="primary" size="medium" @click="savecategory"
        >添加</el-button
      >
      <el-input v-model="input" placeholder="请输入姓名"></el-input>
      {{input}}
      <el-button type="primary" size="medium" @click="searchButton"
        >查询</el-button
      >
    </div>
    <div class="category_center">
      <el-table :data="categoryList" stripe style="width: 100%">
        <el-table-column prop="id" label="编号" width="140"> </el-table-column>
        <el-table-column prop="name" label="栏目名称" width="200">
        </el-table-column>
        <el-table-column prop="num" label="序号" width="140">
        </el-table-column>
        <el-table-column prop="categoryName" label="父栏目" width="440">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <a href="#" @click="deleteById(scope.row)">删除</a>
            <a href="#" @click="updatecategory(scope.row)">编辑</a>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="category_footer">
      <!-- -----------分页------------- -->
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- ----------------添加 编辑功能-------------------------------------- -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="myform">
        <el-form-item
          label="栏目名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="序号"
          :label-width="formLabelWidth"
          prop="num"
        >
         <el-input v-model.number="form.num" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="所属栏目"
          :label-width="formLabelWidth"
          prop="categoryId"
        >
          <el-select v-model="form.parentId" placeholder="请选择活动区域">
            <el-option
              :label="item.name"
              :value="item.id"
              v-for="item in category"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('myform')"
          >确 定</el-button
        > 
      </div> 
    </el-dialog>
  </div>
</template>

  <script>
import Qs from "querystring";
export default {
  data() {
    return {
      //表单内容
      input: "",
      //分页栏目数据
      column:[],
      // 总栏目数据
      category: [],
      // 当前点击分页
      currentPage: 1,
      //实际请求分页
      requirePage: 0,
      // 栏目渲染个数
      pageSize: 10,
      //栏目总数
      total: 0,
      title: "添加栏目信息",
      //模态框
      dialogFormVisible: false,
      form: {
        name: "",
        num: "",
        parentId:""
      },
      formLabelWidth: "100px",
      //表单验证规则
      rules: {
        name: [
          {
            required: true,
            message: "请输入栏目名称",
            trigger: "blur",
          },
          {
            min: 1,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //分页发送请求
    // 形参name用来接收 栏目名称 查询
    column_axios(name) {
      if (name) {
        //查询
        var data = {
          page: this.requirePage,
          pageSize: this.pageSize,
          name: name,
        };
      } else {
        var data = {
          page: this.requirePage,
          pageSize: this.pageSize,
        };
      }
      this.$axios.post("/category/query", Qs.stringify(data)).then((respond) => {
        this.column = respond.data.data.list;
        this.total = respond.data.data.total;
      });
    },
    //查询所有栏目函数
    findAllCategory() {
      return this.$axios.get("/category/findAll");
    },
    // 分页函数
    handleCurrentChange(val) {
      this.requirePage = val - 1;
      this.column_axios();
    },
    // 添加按钮功能
    savecategory() {
      this.form = {
        name: "",
        num: "",
        parentId: "",
      };
      this.dialogFormVisible = true;
      this.title = "添加栏目信息";
    },
    //编辑按钮功能
    updatecategory(row) {
      this.dialogFormVisible = true;
      this.title = "编辑栏目信息";
      this.form = {
        id: row.id,
        name: row.name,
        num: row.num,
        parentId: row.categoryId,
      };
    },
    //添加确定按钮
    submitForm(formName) {
      let that = this;
      //1.获取表单数据,表单验证
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //2.发送请求
          that.$axios
            .post("/category/saveOrUpdate", Qs.stringify(that.form))
            .then((response) => {
              if (response.data.status === 200) {
                this.$message({
                  message: "保存成功",
                  type: "success",
                });
                that.column_axios();
              } else {
                this.$message.error("保存失败");
              }
              that.dialogFormVisible = false;
            });
        } else {
          return false;
        }
      });
    },
    //查询按钮功能,根据input输入的栏目名
    searchButton() {
      if (this.input) {
        this.requirePage = 0;
        this.column_axios(this.input);
      } else {
        this.column_axios();
      }
    },
    //删除按钮
    deleteById(row) {
      let that = this;
      let id = row.id;
      that.$axios
        .get("/category/deleteById", {
          params: {
            id: id,
          },
        })
        .then((respond) => {
          if (respond.data.status === 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.currentPage= 1;
            this.requirePage = 0;
            that.column_axios();
          } else {
            this.$message.error("删除失败");
          }
        });
    },
    //详情按钮
    details(id) {
      //编程式路由传值
      this.$router.push({
        path: "/categoryDetails/index",
        query: {
          id: id,
        },
      });
    },
  },
  computed: {
    categoryList() {
      var that = this;
      //1.获取分页栏目信息
      // console.log(this.column);
      //2.获取总栏目信息
      // console.log(this.category);
      //3.用分页栏目的id匹配总栏目id,将总栏目对象的name赋值给分页栏目对象
      //遍历栏目
      this.column.forEach((item) => {
        //遍历栏目匹配 所属栏目
        for (var i = 0; i < that.category.length; i++) {
          if (item.parentId === that.category[i].id) {
            //将匹配到的栏目name插入到栏目中，渲染到页面
            item.categoryName = that.category[i].name;
          }
        }
      });
      return this.column;
    },
  },
  created() {
      this.column_axios();
    // 查询所有栏目
    this.findAllCategory().then((response) => {
      this.category = response.data.data;
    });
  },
};
</script>

  <style lang="scss" scoped>
.category {
  padding: 20px;

  .category_top .el-input {
    width: 20%;
    margin: 0 10px;
  }

  .category_center a {
    padding-right: 10px;
  }

  .category_center a:hover {
    color: #409eff;
  }

  .el-dialog .note {
    color: #999999;
    font-size: 12px;
  }
}
</style>
